/**
 * 系统管理 轮播图联系管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>轮播图联系管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!--列表-->
    <el-table
      size="small"
      :data="configData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%; margin-top: 2%"
    >
      <el-table-column
        align="center"
        sortable
        prop="value"
        label="轮播图图片"
        width="400"
      >
        <template slot-scope="scope">
          <img :src="scope.row.value" style="height: 50px; width: 100px" />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="sortOrder"
        label="排序"
        min-width="100"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="isShow"
        label="是否显示"
        width="100"
      >
      </el-table-column>
      <el-table-column
        align="center"
        sortable
        prop="updateTime"
        label="修改配置最终时间"
        width="200"
      >
      </el-table-column>
      <el-table-column label="操作" min-width="300" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
            type="primary"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 编辑界面 -->
    <el-dialog
      :title="title"
      :visible.sync="editFormVisible"
      width="60%"
      @click="closeDialog"
    >
      <el-form label-width="90px" :model="editForm" ref="editForm">
        <el-form-item label="轮播图">
          <!-- <el-input size="small" v-model="editForm.value" auto-complete="off" placeholder="请输入轮播图图片"></el-input> -->
          <el-upload
            action="qiniuDomain"
            list-type="picture-card"
            :http-request="upLoadToQiniu"
            :show-file-list="false"
            :before-upload="beforeUpload"
          >
            <img v-if="editForm.value" :src="editForm.value" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="排序">
          <el-input
            size="small"
            v-model="editForm.sortOrder"
            auto-complete="off"
            placeholder="请输入轮播图排序，默认50"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch
            v-model="editForm.isShow == 1 ? nshow : fshow"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="editType"
          >
          </el-switch>
          <!-- <el-input size="small" v-model="editForm.isShow" auto-complete="off" placeholder="电话号码是否显示"></el-input> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm(editForm)"
          >保存</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入请求方法
import Pagination from "../../components/Pagination";
import { configList, updateConfig } from "../../api/systemMg";
import { cdn } from "../../../config/qiniuConfig";
import { uploadToken } from "../../api/uploadMG";
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      title: "查看地址库",
      editFormVisible: false, //控制编辑页面显示与隐藏
      dataAccessshow: false, //控制数据权限显示与隐藏
      unitAccessshow: false, //控制所属单位隐藏与显示
      dialogVisible: false, //图片上传
      // 实名认证后创建的存储空间对应的上传地址(华东，华南等等不一样：https://developer.qiniu.com/kodo/manual/1671/region-endpoint)
      qiniuDomain: cdn.qiniuDomain,
      // 创建成功后官方随机分配的公开图片地址前缀，即上传成功后对应的公用图片地址前缀
      qiniuViewHost: cdn.host,
      dialogImageUrl: "",
      // 编辑与添加
      editForm: {
        id: 0,
        value: "",
        sortOrder: 50,
        isShow: 0,
      },
      // 请求数据参数
      formInline: {
        size: 1,
        current: 10,
      },
      //电话数据
      configData: [],
      pageparm: {
        current: 1,
        size: 10,
        total: 10,
      },
    };
  },
  // 注册组件
  components: {
    Pagination,
  },

  /**
   * 数据发生改变
   */
  watch: {},

  /**
   * 创建完毕
   */
  created() {
    this.formInline.current = 1;
    this.formInline.size = 10;
    let data = {
      pageVo: this.formInline,
      keys: ["SWIPER_IMAGE"],
    };
    this.getdata(data);
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取数据方法
    async getdata(parameter) {
      this.loading = true;
      var that = this;
      // 获取用户列表
      await configList(parameter).then((res) => {
        if (res.code == "200") {
          that.loading = false;
          that.configData = res.data.records;
          that.pageparm.current = res.data.current;
          that.pageparm.size = res.data.size;
          that.pageparm.total = res.data.total;
        } else {
          // that.loading = true
          that.configData = [];
        }
      });
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      let data = {
        pageVo: this.formInline,
        keys: ["SWIPER_IMAGE"],
      };
      this.getdata(this.formInline);
    },

    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      this.editForm = {
        id: row.id,
        value: row.value,
        sortOrder: row.sortOrder,
        isShow: row.isShow,
      };
    },
    // 是否显示按钮处理
    // 修改type
    editType: function () {
      this.editForm.isShow == 1
        ? (this.editForm.isShow = 0)
        : (this.editForm.isShow = 1);
    },

    // 修改配置处理
    async submitForm(data) {
      let that = this;
      // 获取用户列表
      await updateConfig(data).then((res) => {
        if (res.code == "200") {
          that.closeDialog();
          this.$message({
            type: "success",
            message: "电话配置修改成功！",
          });
          let data = {
            pageVo: that.formInline,
            keys: ["SWIPER_IMAGE"],
          };
          this.getdata(data);
        } else {
          // that.loading = true
          that.configData = [];
        }
      });
    },
    // 图片上传
    async upLoadToQiniu(req) {
      const config = {
        headers: { "Content-Type": "multipart/form-data" },
      };
      let fileType = "";
      if (req.file.type === "image/png") {
        fileType = "png";
      } else {
        fileType = "jpg";
      }
      // 重命名要上传的文件
      const keyname = `${new Date().getTime()}${Math.random()
        .toString(36)
        .slice(2)}.${fileType}`;
      // 上传时候的Token,可前端自己生成,安全起见后端生成！
      // 获取Token
      let token = "";
      await uploadToken().then((res) => {
        if (res.code == "200") {
          token = res.data;
        } else {
          // that.loading = true
        }
      });
      const formdata = new FormData();
      formdata.append("file", req.file);
      formdata.append("token", token);
      formdata.append("key", keyname);

      const result = await this.$http.post(this.qiniuDomain, formdata, config);

      // http://qp0e5erxn.hn-bkt.clouddn.com/1614150299156jajg0szk7rs.png
      this.editForm.value = cdn.realHost + "/" + keyname;
    },

    // 上传图片前的校验
    beforeUpload(file) {
      const isJPG =
        file.type === "image/jpeg" ||
        file.type === "image/png" ||
        file.type === "image/jpg";
      const isLt5M = file.size / 1024 / 1024 < 5;

      if (!isJPG) {
        this.$message({
          showClose: true,
          message: "上传图片只能是JPG/PNG 格式!",
          type: "error",
        });
      }
      if (!isLt5M) {
        this.$message({
          showClose: true,
          message: "上传头像图片大小不能超过 5MB!",
          type: "error",
        });
      }
      return isJPG && isLt5M;
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}
.avatar {
  width: 148px;
  height: 148px;
  display: block;
}
</style>

 